package com.yigo.cuc.app.representation;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yigo.cuc.api.customer.query.CustomerListQuery;
import com.yigo.cuc.api.customer.resp.CustomerListResp;
import com.yigo.cuc.app.assembler.CustomerDTO2DmoAssembler;
import com.yigo.cuc.infra.repo.dataobject.CustomerPO;
import com.yigo.cuc.infra.repo.mapper.CustomerMapper;
import com.yigo.framework.http.R;
import com.yigo.framework.http.RPage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author chenzl105
 */
@Service
public class CustomerQueryAppService {
    @Resource
    private CustomerMapper customerMapper;

    public List<CustomerListResp> queryCustomerList(CustomerListQuery query) {
        LambdaQueryWrapper<CustomerPO> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StrUtil.isNotBlank(query.getUserName()), CustomerPO::getUserName, query.getUserName())
                .like(StrUtil.isNotBlank(query.getPhone()), CustomerPO::getPhone, query.getPhone())
                .like(StrUtil.isNotBlank(query.getNickName()), CustomerPO::getNickName, query.getNickName());
        List<CustomerPO> list = customerMapper.selectList(wrapper);
        return CustomerDTO2DmoAssembler.INSTANCE.toListResp(list);
    }

    public R<List<CustomerListResp>> queryCustomerPage(CustomerListQuery query) {
        LambdaQueryWrapper<CustomerPO> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StrUtil.isNotBlank(query.getUserName()), CustomerPO::getUserName, query.getUserName())
                .like(StrUtil.isNotBlank(query.getPhone()), CustomerPO::getPhone, query.getPhone())
                .like(StrUtil.isNotBlank(query.getNickName()), CustomerPO::getNickName, query.getNickName());
        Page<CustomerPO> pagePO = customerMapper.selectPage(new Page<>(query.getPageNum(), query.getPageSize()), wrapper);
        List<CustomerListResp> list = CustomerDTO2DmoAssembler.INSTANCE.toListResp(pagePO.getRecords());
        return R.ok(list, new RPage(pagePO.getSize(), pagePO.getCurrent(), pagePO.getTotal()));
    }
}
